<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/19 13:27
 * Desc: 私人定制模型
 */
class Model_Api_V2_Customize
{
    public function __construct()
    {

    }

    /**
     * 移除订单游客
     * @return void
     */
    public function del_order_tourist($order_id)
    {
        DB::delete('member_order_tourer')->where('orderid', '=', $order_id)->execute();
    }

    /**
     * 移除订单发票
     * @return void
     */
    public function del_order_receipt($order_id)
    {
        DB::delete('member_order_bill')->where('orderid', '=', $order_id)->execute();
    }

    /**
     * 添加订单游客
     * @param $order_id
     * @param array $tourist
     * @return void
     * @throws Kohana_Exception
     */
    public function add_tourist($order_id, array $tourist)
    {
        //游客信息
        foreach ($tourist as $v) {
            $update_data = array(
                'orderid' => $order_id,
                'tourername' => $v['tourername'],
                'sex' => $v['sex'],
                'mobile' => $v['mobile'],
                'cardtype' => $v['cardtype'],
                'cardnumber' => $v['cardnumber'],
            );
            DB::insert('member_order_tourer', array_keys($update_data))->values(array_values($update_data))->execute();
        }
    }

    /**
     * 添加发票
     * @param $order_id
     * @param array $receipt_info
     * @return void
     */
    public function add_receipt($order_id, array $receipt_info)
    {
        //发票
        $invoice_type = $receipt_info['type'];
        $bill_info    = array(
            'title' => $receipt_info['title'],
            'content' => $receipt_info['content'],
            'type' => $invoice_type,
            'taxpayer_number' => $invoice_type != 0 ? $receipt_info['taxpayer_number'] : '',
            'taxpayer_address' => $invoice_type == 2 ? $receipt_info['taxpayer_address'] : '',
            'taxpayer_phone' => $invoice_type == 2 ? $receipt_info['taxpayer_phone'] : '',
            'bank_branch' => $invoice_type == 2 ? $receipt_info['bank_branch'] : '',
            'bank_account' => $invoice_type == 2 ? $receipt_info['bank_account'] : ''
        );

        // 发票地址
        if (isset($receipt_info['address']) && !empty($receipt_info['address'])) {
            $receipt_address       = $receipt_info['address'];
            $bill_info['mobile']   = $receipt_address['phone'];
            $bill_info['receiver'] = $receipt_address['receiver'];
            $bill_info['postcode'] = $receipt_address['postcode'];
            $bill_info['province'] = $receipt_address['province'];
            $bill_info['city']     = $receipt_address['city'];
            $bill_info['address']  = $receipt_address['address'];
        }

        Model_Member_Order_Bill::add_bill_info($order_id, $bill_info);
    }

}